const express = require('express')//库
const app = express()
const path = require('path')
app.listen(3000, () => {
    console.log('端口3000运行成功')
})
// url为login 进行渲染页面
app.get('/login', (req, res) => {
    res.sendFile(path.join(__dirname, 'login.html'))
})
app.get('/register', (req, res) => {
    res.sendFile(path.join(__dirname, 'register.html'))
})
// 页面提交后为gologin 进行数据判断
const fs = require('fs')
// const { parse } = require('path')
let arr = fs.readFileSync('persons.json', 'utf-8')
arr = JSON.parse(arr)
app.get('/gologin', (req, res) => {
    username = req.query.username;
    password = req.query.password;
    let a = arr.find(item => username === item.username && password === item.password)
    if (a) {
        res.send('登录成功')
    } else {
        res.send('用户名密码错误')
    }
})
app.get('/goregister', (req, res) => {
    username = req.query.username;
    password = req.query.password;
    let a = arr.find(item => username === item.username)
    if (a) {
        res.send('用户名已被注册')
    } else {
        let lujing = path.join(__dirname, 'persons.json')
        arr.push(req.query)
        fs.writeFileSync(lujing, (JSON.stringify(arr)))
        res.send('注册成功')
    }
})